﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using C1.Win.C1Input;
using C1.Win.C1TrueDBGrid;
using log4net;
using PCSComProduction.WorkOrder.BO;
using PCSComProduction.WorkOrder.DS;
using PCSComUtils.Common;
using PCSComUtils.DataContext;
using PCSComUtils.PCSExc;
using PCSUtils.Log;
using PCSUtils.Utils;

namespace PCSProduction.WorkOrder
{
    public partial class IssueMaterial : Form
    {
        private ILog _logger = LogManager.GetLogger(typeof (IssueMaterial));
        private const string This = "PCSProduction.WorkOrder.IssueMaterial";
        IssueMaterialBO _boIssueMaterial = new IssueMaterialBO();
        private PRO_IssueMaterialMaster _issueMaster;
        private DataTable _gridLayOut;
        private EnumAction _formMode;
        private List<IssueMaterialDetail> _dataSource = new List<IssueMaterialDetail>();

        public IssueMaterial()
        {
            InitializeComponent();
        }

        private void IssueMaterial_Load(object sender, EventArgs e)
        {
            const string methodName = This + ".IssueMaterial_Load()";
            try
            {
                var objSecurity = new Security();
                Name = This;

                if (objSecurity.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
                {
                    // You don't have the right to view this item
                    PCSMessageBox.Show(ErrorCode.MESSAGE_YOU_HAVE_NO_RIGHT_TO_VIEW, MessageBoxIcon.Warning);
                    Close();
                    return;
                }

                // store the gridlayout
                _gridLayOut = FormControlComponents.StoreGridLayout(DetailGrid);

                // default form mode
                _formMode = EnumAction.Default;

                // Format Post Date Picker
                PostDatePicker.FormatType = FormatTypeEnum.CustomFormat;
                PostDatePicker.CustomFormat = Constants.DATETIME_FORMAT_HOUR;

                PrintButton.Click += FormControlComponents.RunDefaultReportEntriesHandler;

                SwitchFormMode(_formMode);
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                _logger.Error(ex.CauseException.Message, ex.CauseException);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message, ex);
            }
        }

        private void IssueNoInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string methodName = This + ".IssueNoInput_Validating()";
            try
            {
                if (!SearchIssueButton.Enabled || !IssueNoInput.Modified)
                {
                    return;
                }
                if (string.IsNullOrEmpty(IssueNoInput.Text.Trim()))
                {
                    BindMasterInfo(null);
                    return;
                }

                var searchResult = FormControlComponents.OpenSearchForm(v_PROIssueWithFGoodsCode.TABLE_NAME, PRO_IssueMaterialMasterTable.ISSUENO_FLD, IssueNoInput.Text.Trim(), null, false);
                if (searchResult != null)
                {
                    BindMasterInfo(searchResult);
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void IssueNoInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (SearchIssueButton.Enabled && e.KeyCode == Keys.F4)
            {
                SearchIssueButton_Click(null, null);
            }
        }

        private void SearchIssueButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".SearchIssueButton_Click()";
            try
            {
                var searchResult = FormControlComponents.OpenSearchForm(v_PROIssueWithFGoodsCode.TABLE_NAME, PRO_IssueMaterialMasterTable.ISSUENO_FLD, IssueNoInput.Text.Trim(), null, true);
                if (searchResult != null)
                {
                    BindMasterInfo(searchResult);
                }
                else
                {
                    IssueNoInput.Focus();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void MasLocInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string methodName = This + ".MasLocInput_Validating()";
            try
            {
                if (!MasLocButton.Enabled || !MasLocInput.Modified)
                {
                    return;
                }
                if (string.IsNullOrEmpty(MasLocInput.Text.Trim()))
                {
                    BindMasterLocation(null);
                    return;
                }

                var searchResult = FormControlComponents.OpenSearchForm(v_MasterLocationNoSecurity.TABLE_NAME, v_MasterLocationNoSecurity.CODE_FLD, MasLocInput.Text.Trim(), null, false);
                if (searchResult != null)
                {
                    BindMasterLocation(searchResult);
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void MasLocInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (MasLocButton.Enabled && e.KeyCode == Keys.F4)
            {
                MasLocButton_Click(null, null);
            }
        }

        private void MasLocButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".MasLocButton_Click()";
            try
            {
                var searchResult = FormControlComponents.OpenSearchForm(v_MasterLocationNoSecurity.TABLE_NAME, v_MasterLocationNoSecurity.CODE_FLD, MasLocInput.Text.Trim(), null, true);
                if (searchResult != null)
                {
                    BindMasterLocation(searchResult);
                }
                else
                {
                    MasLocInput.Focus();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void LocationInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string methodName = This + ".LocationInput_Validating()";
            try
            {
                if (!SearchLocationButton.Enabled || !LocationInput.Modified)
                {
                    return;
                }
                if (string.IsNullOrEmpty(LocationInput.Text.Trim()))
                {
                    BindLocation(null);
                    return;
                }

                if (MasLocInput.Tag == null)
                {
                    string[] msg = { MasLocLabel.Text, LocationLabel.Text };
                    PCSMessageBox.Show(ErrorCode.MESSAGE_SELECT_ONE_BEFORE_SELECT_ONE, MessageBoxIcon.Warning, msg);
                    // clear current input and focus to master location
                    LocationInput.Text = string.Empty;
                    MasLocInput.Focus();
                    return;
                }

                var criterials = new Hashtable {{MST_LocationTable.MASTERLOCATIONID_FLD, MasLocInput.Tag}};

                var searchResult = FormControlComponents.OpenSearchForm(v_LocationNoScecurity.TABLE_NAME, v_LocationNoScecurity.CODE_FLD, LocationInput.Text.Trim(), criterials, false);
                if (searchResult != null)
                {
                    BindLocation(searchResult);
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void LocationInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (SearchLocationButton.Enabled && e.KeyCode == Keys.F4)
            {
                SearchLocationButton_Click(null, null);
            }
        }

        private void SearchLocationButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".SearchLocationButton_Click()";
            try
            {
                if (MasLocInput.Tag == null)
                {
                    string[] msg = { MasLocLabel.Text, LocationLabel.Text };
                    PCSMessageBox.Show(ErrorCode.MESSAGE_SELECT_ONE_BEFORE_SELECT_ONE, MessageBoxIcon.Warning, msg);
                    // clear current input and focus to master location
                    LocationInput.Text = string.Empty;
                    MasLocInput.Focus();
                    return;
                }

                var criterials = new Hashtable { { MST_LocationTable.MASTERLOCATIONID_FLD, MasLocInput.Tag } };

                var searchResult = FormControlComponents.OpenSearchForm(v_LocationNoScecurity.TABLE_NAME, v_LocationNoScecurity.CODE_FLD, LocationInput.Text.Trim(), criterials, true);
                if (searchResult != null)
                {
                    BindLocation(searchResult);
                }
                else
                {
                    LocationInput.Focus();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void BinInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string methodName = This + ".BinInput_Validating()";
            try
            {
                if (!SearchBinButton.Enabled || !BinInput.Modified)
                {
                    return;
                }
                if (string.IsNullOrEmpty(BinInput.Text.Trim()))
                {
                    BindBin(null);
                    return;
                }

                if (LocationInput.Tag == null)
                {
                    string[] msg = { LocationLabel.Text, BinLabel.Text };
                    PCSMessageBox.Show(ErrorCode.MESSAGE_SELECT_ONE_BEFORE_SELECT_ONE, MessageBoxIcon.Warning, msg);
                    // clear current input and focus to location
                    BinInput.Text = string.Empty;
                    LocationInput.Focus();
                    return;
                }

                var criterials = new Hashtable { { MST_BINTable.LOCATIONID_FLD, LocationInput.Tag } };

                var searchResult = FormControlComponents.OpenSearchForm(v_BinNoSecurity.TABLE_NAME, v_BinNoSecurity.CODE_FLD, BinInput.Text.Trim(), criterials, false);
                if (searchResult != null)
                {
                    BindBin(searchResult);
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void BinInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (SearchBinButton.Enabled && e.KeyCode == Keys.F4)
            {
                SearchBinButton_Click(null, null);
            }
        }

        private void SearchBinButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".SearchBinButton_Click()";
            try
            {
                if (LocationInput.Tag == null)
                {
                    string[] msg = { LocationLabel.Text, BinLabel.Text };
                    PCSMessageBox.Show(ErrorCode.MESSAGE_SELECT_ONE_BEFORE_SELECT_ONE, MessageBoxIcon.Warning, msg);
                    // clear current input and focus to location
                    BinInput.Text = string.Empty;
                    LocationInput.Focus();
                    return;
                }

                var criterials = new Hashtable { { MST_BINTable.LOCATIONID_FLD, LocationInput.Tag } };

                var searchResult = FormControlComponents.OpenSearchForm(v_BinNoSecurity.TABLE_NAME, v_BinNoSecurity.CODE_FLD, BinInput.Text.Trim(), criterials, true);
                if (searchResult != null)
                {
                    BindBin(searchResult);
                }
                else
                {
                    BinInput.Focus();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void PurposeInput_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            const string methodName = This + ".PurposeInput_Validating()";
            try
            {
                if (!SearchPurposeButton.Enabled || !PurposeInput.Modified)
                {
                    return;
                }
                if (string.IsNullOrEmpty(PurposeInput.Text.Trim()))
                {
                    BindPurpose(null);
                    return;
                }
                int tranTypeId = 0;
                if (SystemProperty.TransactionType != null && SystemProperty.TransactionType.Count > 0)
                {
                    var tranType = SystemProperty.TransactionType.FirstOrDefault(t => t.Code == TransactionTypeEnum.PROIssueMaterial.ToString());
                    if (tranType != null)
                    {
                        tranTypeId = tranType.TranTypeID;
                    }
                }

                Hashtable criterials = null;
                if (tranTypeId > 0)
                {
                    criterials = new Hashtable { { PRO_IssuePurposeTable.TRANTYPEID_FLD, tranTypeId } };
                }

                var searchResult = FormControlComponents.OpenSearchForm(PRO_IssuePurposeTable.TABLE_NAME, PRO_IssuePurposeTable.DESCRIPTION_FLD, PurposeInput.Text.Trim(), criterials, false);
                if (searchResult != null)
                {
                    BindPurpose(searchResult);
                }
                else
                {
                    e.Cancel = true;
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void PurposeInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (SearchPurposeButton.Enabled && e.KeyCode == Keys.F4)
            {
                SearchPurposeButton_Click(null, null);
            }
        }

        private void SearchPurposeButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".SearchPurposeButton_Click()";
            try
            {
                int tranTypeId = 0;
                if (SystemProperty.TransactionType != null && SystemProperty.TransactionType.Count > 0)
                {
                    var tranType = SystemProperty.TransactionType.FirstOrDefault(t => t.Code == TransactionTypeEnum.PROIssueMaterial.ToString());
                    if (tranType != null)
                    {
                        tranTypeId = tranType.TranTypeID;
                    }
                }

                Hashtable criterials = null;
                if (tranTypeId > 0)
                {
                    criterials = new Hashtable { { PRO_IssuePurposeTable.TRANTYPEID_FLD, tranTypeId } };
                }

                var searchResult = FormControlComponents.OpenSearchForm(PRO_IssuePurposeTable.TABLE_NAME, PRO_IssuePurposeTable.DESCRIPTION_FLD, PurposeInput.Text.Trim(), criterials, false);
                if (searchResult != null)
                {
                    BindPurpose(searchResult);
                }
                else
                {
                    PurposeInput.Focus();
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void SelectComponentButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".SelectComponentButton_Click()";
            try
            {
                var selectIssueForm = new SelectIssueComponent();
                var dialogResult = selectIssueForm.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    // rebind data to data grid
                    foreach (var component in selectIssueForm.SelectedComponents)
                    {
                        if (_dataSource.Any(item => item.ProductID == component.ProductID && item.WorkOrderDetailID == component.WorkOrderDetailID))
                        {
                            continue;
                        }
                        _dataSource.Add(component);
                    }
                    DetailGrid.Rebind(true);
                }
            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void DetailGrid_AfterColUpdate(object sender, ColEventArgs e)
        {
            const string methodName = This + ".DetailGrid_AfterColUpdate()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void DetailGrid_AfterDelete(object sender, EventArgs e)
        {
            const string methodName = This + ".DetailGrid_AfterDelete()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void DetailGrid_BeforeColEdit(object sender, BeforeColEditEventArgs e)
        {
            const string methodName = This + ".DetailGrid_BeforeColEdit()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void DetailGrid_BeforeColUpdate(object sender, BeforeColUpdateEventArgs e)
        {
            const string methodName = This + ".DetailGrid_BeforeColUpdate()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void DetailGrid_ButtonClick(object sender, ColEventArgs e)
        {
            const string methodName = This + ".DetailGrid_ButtonClick()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void DetailGrid_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void DetailGrid_OnAddNew(object sender, EventArgs e)
        {
            const string methodName = This + ".DetailGrid_OnAddNew()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".AddButton_Click()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".SaveButton_Click()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            const string methodName = This + ".DeleteButton_Click()";
            try
            {

            }
            catch (PCSException ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex.CauseException, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                // displays the error message.
                PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
                // log message.
                try
                {
                    Logger.LogMessage(ex, methodName, Level.ERROR);
                }
                catch
                {
                    PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
                }
            }
        }

        private void PrintButton_EnabledChanged(object sender, EventArgs e)
        {
            PrintConfigurationButton.Enabled = PrintButton.Enabled;
        }

        private void SwitchFormMode(EnumAction formMode)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     Bind data source to grid and format the layout
        /// </summary>
        private void BindGrid()
        {
            
        }

        /// <summary>
        ///     Bind the issue master information to controls on form
        /// </summary>
        private void BindMasterInfo(DataRowView issueMasterRow)
        {
            var issueMasterId = (int) issueMasterRow[PRO_IssueMaterialMasterTable.ISSUEMATERIALMASTERID_FLD];
            _issueMaster = _boIssueMaterial.GetIssueMaster(issueMasterId);
        }

        private void BindMasterLocation(DataRowView searchResult)
        {
            
        }

        private void BindLocation(DataRowView searchResult)
        {
            
        }

        private void BindBin(DataRowView searchResult)
        {
            
        }

        private void BindPurpose(DataRowView searchResult)
        {
            
        }

        /// <summary>
        ///     Validate the input data on form
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            return true;
        }

        /// <summary>
        ///     Reset all data to default value
        /// </summary>
        private void ResetForm()
        {

        }
    }
}
